#!/bin/sh

################################################################################
# Copyright 1999-2010 by Nice, srl.,
# Via Milliavacca, 9
# 14100 Asti (AT) - ITALY
# All rights reserved.
#
# This software is the confidential and proprietary information
# of Nice, srl. ("Confidential Information").
# You shall not disclose such Confidential Information
# and shall use it only in accordance with the terms of
# the license agreement you entered into with Nice.
################################################################################

. "${EF_ROOT}/plugins/ef/bin/common"

#-------------------------------------------------------------------------------
# parses ouput of ls (or a command providing similar data) given on stdin and
# produces the corresponding xml
#-------------------------------------------------------------------------------
while [ $# -gt 0 ]; do
  case "$1" in
  --vroot|--path|--target|--sortby|--filter|--sorted|--filtered)
    _var=`echo $1 | sed 's/^--//'`
    _val=`ef_xml_escape -a -i "$2"`
    eval "${_var}=\"${_val}\""
    shift; shift
    ;;
  *)
    shift
    ;;
  esac
done

CURRENT_YEAR=`date '+%Y'`

# must be in the format +NN:MM but date '+%:z' is not portable
#FIXME la timezone non è corretta perchè è quella del client non del server irods!
TIMEZONE=`date '+%z' | sed 's/\([0-9]\{2\}$\)/:\1/'`

# ---[ ASCII Character ]---
# Oct=001, Dec=1, Hex=01 - Char="SOH (start of heading)" - ctrl="^A"
_ascii_001=`printf "\001"`

# ---[ ASCII Character ]---
# Oct=010, Dec=8, Hex=08 - Char="(BS  ‚Äö√Ñ√¥\b‚Äö√Ñ√¥ (backspace)" - ctrl="^H"
_backspace=`printf "\b"`

echo "<fm:items xmlns:fm=\"http://www.enginframe.com/2009/fm\" "
echo "          vroot=\"${vroot}\" path=\"${path}\" "
echo "          sort-by=\"${sortby}\" filter=\"${filter}\" "
echo "          max-results=\"-1\""
echo "          sorted=\"${sorted}\" filtered=\"${filtered}\">"

(cat /dev/stdin | \
sed "s/[${_backspace}${_ascii_001}]//g" | \
ef_xml_escape -a --pipe | \
"${EF_AWK}" -v MYTIMEZONE="${TIMEZONE}" -v PRINT_YEAR="${CURRENT_YEAR}" \
       -v target="${target}" \
       -f "${EF_ROOT}/plugins/irods/lib/ils2xml.awk"
) 2>/dev/null

echo "</fm:items>"
